package com.kate.mall.product.service.impl;

import com.kate.mall.product.dto.StockMsgDTO;
import com.kate.mall.product.entity.GoodsEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kate.mall.common.utils.PageUtils;
import com.kate.mall.common.utils.Query;

import com.kate.mall.product.dao.GoodsProductDao;
import com.kate.mall.product.entity.GoodsProductEntity;
import com.kate.mall.product.service.GoodsProductService;
import org.springframework.transaction.annotation.Transactional;

import static com.kate.mall.common.constants.RabbitMessageConstants.*;
@Service("goodsProductService")
@Slf4j
public class GoodsProductServiceImpl extends ServiceImpl<GoodsProductDao, GoodsProductEntity> implements GoodsProductService {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsProductEntity> page = this.page(
                new Query<GoodsProductEntity>().getPage(params),
                generatorWrapper(params)
        );

        return new PageUtils(page);
    }
    private  QueryWrapper<GoodsProductEntity> generatorWrapper(Map<String, Object> params){
        QueryWrapper<GoodsProductEntity> queryWrapper = new QueryWrapper<>();
        if(params.containsKey("goods_id")){
            queryWrapper.lambda().eq(GoodsProductEntity::getGoodsId,Integer.parseInt(String.valueOf(params.get("goods_id"))));
        }
        //TODO 要不要写逻辑删除条件？？？
        return queryWrapper;
    }
    /**
     * 减少库存
     * @param productId
     * @param reduceNumber
     * @return
     */
    @Transactional
    public boolean reduceStock(Integer orderId,Integer productId, Short reduceNumber){

        log.info("======={}，减少库存为：{}=====",productId,reduceNumber);
        Integer remainNumber  = this.getById(productId).getNumber()-reduceNumber;
        boolean result = this.updateById(new GoodsProductEntity().setId(productId).setNumber(remainNumber));
//        if(result){//减库存成功
//            log.info("=====发送锁定库存的ttl消息======");
//            //TODO 发送减库存消息
//            StockMsgDTO stockMsgDto = new StockMsgDTO();
//            stockMsgDto.setProductId(productId);
//            stockMsgDto.setLockNumber(reduceNumber);
//            stockMsgDto.setOrderId(orderId);
//            rabbitTemplate.convertAndSend(STOCK_EVENT_EXCHANGE,STOCK_LOCK_DLK,stockMsgDto);
//        }
        return result;
    }
    /**
     * 释放库存
     * @param stockMsgDto
     * @return
     */
    @Transactional
    public boolean  releaseStock(StockMsgDTO stockMsgDto) {
        log.info("======={}，释放库存为：{}=====",stockMsgDto.getProductId(),stockMsgDto.getLockNumber());
        Integer remainNumber  = this.getById(stockMsgDto.getProductId()).getNumber()+stockMsgDto.getLockNumber();
        boolean result = this.updateById(new GoodsProductEntity().setId(stockMsgDto.getProductId()).setNumber(remainNumber));
        if(result){//减库存成功
            log.info("=====释放库存的成功======");
        }
        return result;
    }

}